package com.atguigu.day08.p2;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/8 11:10
 * @注释
 */
public class StudyTest {
    public static void main(String[] args) {
        //创建学生对象并放进对象数组中
        Study[] sArr = getStudies();
        //排序前
        showInfo("排序前:", sArr);
        //将学生按照成绩从小到大进行排序
        sort(sArr);
        //排序后
        showInfo("排序后:", sArr);
    }

    //排序操作
    private static void sort(Study[] sArr) {
        for (int i = 0; i < sArr.length - 1; i++) {
            for (int j = 0; j < sArr.length - 1; j++) {
                if(sArr[j].gander > sArr[j + 1].gander){
                    Study temp = sArr[j];
                    sArr[j] = sArr[j + 1];
                    sArr[j + 1] = temp;
                }
            }
        }
    }

    //展示排列前后信息
    private static void showInfo(String massage, Study[] sArr) {
        System.out.println(massage);
        for(Study s: sArr){
            String info = s.getInfo();
            System.out.println(info);
        }
    }

    //创建学生对象,并放入对象数组中
    private static Study[] getStudies() {
        Study s1 = new Study();
        s1.name = "张三";
        s1.age = 18;
        s1.gander = 95.5;

        Study s2 = new Study();
        s2.name = "李四";
        s2.age = 19;
        s2.gander = 90.5;

        Study s3 = new Study();
        s3.name = "王五";
        s3.age = 18;
        s3.gander = 70.5;

        Study s4 = new Study();
        s4.name = "赵六";
        s4.age = 18;
        s4.gander = 86.5;

        Study s5 = new Study();
        s5.name = "孙七";
        s5.age = 18;
        s5.gander = 99.9;

        Study[] sArr = new Study[5];
        sArr[0] = s1;
        sArr[1] = s2;
        sArr[2] = s3;
        sArr[3] = s4;
        sArr[4] = s5;
        return sArr;
    }
}
